package com.jeeplus.modules.activity.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.process.AchievementEnum;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.activity.entity.Activity;
import com.jeeplus.modules.activity.entity.ActivityEchartVo;
import com.jeeplus.modules.activity.service.ActivityService;
import com.jeeplus.modules.kpi.entity.AchievementInfo;
import com.jeeplus.modules.process.entity.ProcessInfo;
import com.jeeplus.modules.process.service.ProcessInfoService;
import com.jeeplus.modules.project.entity.BaseProjectInfo;
import com.jeeplus.modules.project.entity.ProjectApply;
import com.jeeplus.modules.sys.utils.UserUtils;

/**
 * 活动管理Controller
 * @author lw
 * @version 2018-01-14
 */
@Controller
@RequestMapping(value = "${adminPath}/activity/activityInfo")
public class ActivityInfoController extends BaseController {

	@Autowired
	private ActivityService activityService;
	
	@Autowired
	private ProcessInfoService processInfoService;
	
	@ModelAttribute
	public Activity get(@RequestParam(required=false) String id) {
		Activity entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = activityService.get(id);
		}
		if (entity == null){
			entity = new Activity();
		}
		return entity;
	}
	
	/**
	 * 活动信息列表页面
	 */
	//@RequiresPermissions("activity:activity:list")
	@RequestMapping(value = {"list", ""})
	public String list(Activity activity, HttpServletRequest request, HttpServletResponse response, Model model) {
		activity.setUser(UserUtils.getUser());
		Page<Activity> page = activityService.findPage(new Page<Activity>(request, response), activity); 
		model.addAttribute("page", page);
		return "modules/activity/activityInfoList";
	}

	/**
	 * 查看，增加，编辑活动信息表单页面
	 */
	@RequestMapping(value = "add")
	public String form(Activity activity, Model model) {
		activityService.getActRecordList(activity);
		model.addAttribute("activity", activity);
		return "modules/activity/activityInfoAdd";
	}
	

	/**
	 * 活动审批
	 * @param activity
	 * @param model
	 * @return
	 */
	@RequiresPermissions(value={"activity:activity:view","activity:activity:add","activity:activity:edit"},logical=Logical.OR)
	@RequestMapping(value = "activityInfoAprovForm")
	public String activityInfoAprovForm(Activity activity, Model model) {
		if (StringUtils.isNotBlank(activity.getId())){
			activityService.getActRecordList(activity);
		}
		String view = "add";
		// 查看审批申请单
		if (StringUtils.isNotBlank(activity.getId())){
			// 环节编号
			String taskDefKey = activity.getAct().getTaskDefKey();
			// 查看工单
			if(activity.getAct().isFinishTask()){
				view = "activityInfoAprovForm";
			}
			else if ("dept-review".equals(taskDefKey)){
				view = "activityInfoAprovForm";
			}
			else if ("dept-leader-review".equals(taskDefKey)){
				view = "activityInfoAprovForm";
			}
			else if ("sci-research-magt-review".equals(taskDefKey)){
				view = "activityInfoAprovForm";
			}
			else if ("big-boss-review".equals(taskDefKey)){
				view = "activityInfoAprovForm";
			}
			else if ("usertask2".equals(taskDefKey)){
				view = "activityInfoSetUpFrom";
			}
			else if("start-apply".equals(taskDefKey)){
				view = "activityInfoView";
			}
		}
		model.addAttribute("activity", activity);
		return "modules/activity/"+view;
	}
	
	@RequiresPermissions("activity:activity:edit")
	@RequestMapping(value = "saveAprov")
	public String saveAprov(Activity activity, Model model) {
		if (StringUtils.isBlank(activity.getAct().getFlag())
				|| StringUtils.isBlank(activity.getAct().getComment())){
			addMessage(model, "请填写审核意见。");
			return activityInfoAprovForm(activity, model);
		}
		activityService.approveSave(activity);
		return "redirect:" + adminPath + "/act/task/todo/";
	}
	
	/**
	 * 保存活动信息
	 */
	//@RequiresPermissions(value={"activity:activity:add","activity:activity:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Activity activity, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response) throws Exception{
		if (!beanValidator(model, activity)){
			return form(activity, model);
		}
		
		boolean  edit = false;
		if(!activity.getIsNewRecord()){//编辑表单保存
			Activity t = activityService.get(activity.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(activity, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			activityService.save(t);//保存
			edit = true;
		}else{//新增表单保存
			activity.setUser(UserUtils.getUser());
			activity.setOffice(UserUtils.getUser().getOffice());
			activity.setProcessNode("1");
			activityService.save(activity);//保存
			
			// 保存流程节点
			/*String id = activity.getId();
			if (!StringUtils.isEmpty(id)) {
				ProcessInfo processInfo = new ProcessInfo();
				processInfo.setProcessId(id);
				processInfo.setProcessIndex(AchievementEnum.DEPARTMENT.getProcess());
				processInfo.setProcessText("等待部门审核");
				processInfoService.save(processInfo);
			}*/
		}
		addMessage(redirectAttributes, "提交活动调研信息成功");
		//return "redirect:"+Global.getAdminPath()+"/activity/activity/?repage";
	
			activity.setCreateBy(UserUtils.getUser());
			Page<Activity> page = activityService.findPage(new Page<Activity>(request, response), activity); 
			model.addAttribute("page", page);
			if(edit){
				return "redirect:" + adminPath + "/act/task/todo/";
			}
			return "redirect:"+Global.getAdminPath()+"/activity/activityInfo/list";	
			
	}
	
	/**
	 * 查看，增加，编辑活动信息表单页面
	 */
	@RequiresPermissions(value={"activity:activity:view","activity:activity:add","activity:activity:edit"},logical=Logical.OR)
	@RequestMapping(value = "view")
	public String view(Activity activity, Model model) {
		if (StringUtils.isNotBlank(activity.getId())){
			activityService.getActRecordList(activity);
		}
		model.addAttribute("activity", activity);
		return "modules/activity/activityInfoView";
	}
	//-------------------------------
	@RequiresPermissions(value={"activity:activity:add","activity:activity:edit"},logical=Logical.OR)
	@RequestMapping(value = "saveFilePath")
	public String saveFilePath(Activity activity, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, activity)){
			return form(activity, model);
		}
			activityService.saveFilePath(activity);//保存
		addMessage(redirectAttributes, "保存附件成功");
		return "redirect:"+Global.getAdminPath()+"/activity/activityInfo/?repage";
	}
	
	/**
	 * 删除活动信息
	 */
	@RequiresPermissions("activity:activity:del")
	@RequestMapping(value = "delete")
	public String delete(Activity activity, RedirectAttributes redirectAttributes) {
		activityService.delete(activity);
		addMessage(redirectAttributes, "删除活动信息成功");
		return "redirect:"+Global.getAdminPath()+"/activity/activity/?repage";
	}
	
	/**
	 * 批量删除活动信息
	 */
	@RequiresPermissions("activity:activity:del")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			activityService.delete(activityService.get(id));
		}
		addMessage(redirectAttributes, "删除活动信息成功");
		return "redirect:"+Global.getAdminPath()+"/activity/activity/?repage";
	}					     
	@RequestMapping(value = "echartView",method=RequestMethod.POST)
	@ResponseBody
	public ActivityEchartVo echartView(String officeId) {
		ActivityEchartVo echartView = activityService.echartView(officeId);
		return echartView;
	}
	@RequestMapping(value = "echartAllView",method=RequestMethod.GET)
	@ResponseBody
	public ActivityEchartVo echartAllView() {
		ActivityEchartVo echartView = activityService.echartAllView();
		return echartView;
	}
	
}